package pms.qa.service;

import java.io.File;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.springframework.jdbc.support.rowset.SqlRowSet;

import pms.common.value.QaListValue;
import pms.common.dao.ListSGDao;
import pms.common.dao.QaDetailDao;

public class QaDetailServiceImpl implements QaDetailService {

	private String filename;
	private InputStream is;
	private int currentStatus;
	private String[] limitDateSub;
	private String[] confirmDateSub;
	private QaDetailDao qaDetailDao;
	private ListSGDao listSGDao;
	
	public ListSGDao getListSGDao() {
		return listSGDao;
	}

	public void setListSGDao(ListSGDao listSGDao) {
		this.listSGDao = listSGDao;
	}

	public int getCurrentStatus() {
		return currentStatus;
	}

	public void setCurrentStatus(int currentStatus) {
		this.currentStatus = currentStatus;
	}

	public String[] getLimitDateSub() {
		return limitDateSub;
	}

	public void setLimitDateSub(String[] limitDateSub) {
		this.limitDateSub = limitDateSub;
	}

	public String[] getConfirmDateSub() {
		return confirmDateSub;
	}

	public void setConfirmDateSub(String[] confirmDateSub) {
		this.confirmDateSub = confirmDateSub;
	}

	public QaDetailDao getQaDetailDao() {
		return qaDetailDao;
	}

	public void setQaDetailDao(QaDetailDao qaDetailDao) {
		this.qaDetailDao = qaDetailDao;
	}
	
	public String getFilename() {
		return qaDetailDao.getFilename();
	}

	public InputStream getIs() {
		return qaDetailDao.getIs();
	}
	
	public QaListValue getListdetailData(String NO) throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {

		QaListValue detailList = new QaListValue();
		HashMap proList = listSGDao.getProject();
		HashMap themeList = listSGDao.getTheme();
		SqlRowSet l_detailListData = qaDetailDao.getListdetailData(NO);
		if(l_detailListData.next()){
			detailList.setBanGo(NO);
			detailList.setProject(l_detailListData.getString("PROJECTID"));
			detailList.setTema(l_detailListData.getString("TEMA"));
			if(proList.containsKey(String.valueOf(l_detailListData.getString("PROJECTID").trim()))){
				detailList.setProject(String.valueOf(proList.get(l_detailListData.getString("PROJECTID").trim())));
    		} else {
    			detailList.setProject(l_detailListData.getString("PROJECTID").trim());
    		}
    		if(themeList.containsKey(String.valueOf(l_detailListData.getString("TEMA").trim()))){
    			detailList.setTema(String.valueOf(themeList.get(l_detailListData.getString("TEMA").trim())));
    		} else {
    			detailList.setTema(l_detailListData.getString("TEMA").trim());
    		}
			detailList.setDankai(l_detailListData.getString("QA_LEVEL"));
			detailList.setKasho(l_detailListData.getString("QA_PART"));
			detailList.setGaIYo(l_detailListData.getString("QA_SUMMARY"));
			detailList.setInputDate(dateFormat(l_detailListData.getString("QA_START_DATE")));
			detailList.setAsker(l_detailListData.getString("QA_FROM"));
			detailList.setAnswerDate(dateFormat(l_detailListData.getString("QA_ANSWER_DATE")));
			detailList.setEmergencyDegree(l_detailListData.getString("QA_KINKYUU"));
			detailList.setToIAWaSeSaKi(l_detailListData.getString("QA_TO"));
			detailList.setConfirmer(l_detailListData.getString("QA_CONFIRMER"));
			detailList.setStatus(l_detailListData.getString("QA_STATUS"));
			detailList.setSelBanGo(l_detailListData.getString("QA_QUESTION"));
			detailList.setNaiYo(l_detailListData.getString("QA_ANSWER"));
			detailList.setBiko(l_detailListData.getString("QA_MEMO"));
			detailList.setFromShiRyouname(l_detailListData.getString("QA_FILE_FROM_NM"));
			detailList.setToShiRyouname(l_detailListData.getString("QA_FILE_TO_NM"));
			
			currentStatus = getStatus(l_detailListData.getString("QA_STATUS").trim());
			limitDateSub = dateSub(l_detailListData.getString("QA_LIMIT_DATE").trim());
			confirmDateSub = dateSub(l_detailListData.getString("QA_CONFIRM_DATE").trim());
			
		}
		return detailList;
	}
	
	public void getBlobData(String NO,String direction) throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
		System.out.println("bango :"+NO);
		System.out.println("direction :"+direction);
		qaDetailDao.getBlobData(NO, direction);
	}
	
	
	//public void updateListdetailData(final File file, final String fileName, final String STATUS,final String KINKYUU, final String TO,final String QUESTION, final String ANSWER,final String LIMIT_DATE,final String ANSWER_DATE,final String CONFIRM_DATE, final String CONFIRMER,final String MEMO, final String NO) throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
	public void updateListdetailData(final File file, final String fileName, final QaListValue detailList, final String limitDate, final String currentDate, final String confirmDate) throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
		qaDetailDao.updateListdetailData(file, fileName, detailList, limitDate, currentDate, confirmDate);
	}
	@SuppressWarnings("unchecked")
	public List<HashMap> getMaterArrayList(String l_key) throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
		 List<HashMap> l_array = new ArrayList();
        SqlRowSet l_rs = listSGDao.getMasterData(l_key);
        while(l_rs.next()) {
        	HashMap l_hashMap = new HashMap();
        	l_hashMap.put("master_id",l_rs.getString("MASTER_ID").trim());
        	l_hashMap.put("master_group",l_rs.getString("MASTER_GROUP").trim());
        	l_hashMap.put("master_value",l_rs.getString("MASTER_VALUE").trim());
        	l_array.add(l_hashMap);
         }
        return l_array;
	}
	// 問合先
	@SuppressWarnings("unchecked")
	public List<HashMap> getAskedArray() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {   
		List<HashMap> l_array = new ArrayList();
		l_array = listSGDao.getUserData();
		return l_array;
    }
	
	public String dateFormat(String datestr) {
		String[] strtemp = new String[3];
		String getDateStr = "";
		
		if(datestr.length()==8){
			strtemp[0] = datestr.substring(0,4);
			strtemp[1] = datestr.substring(4,6);
			strtemp[2] = datestr.substring(6,8);
		getDateStr = strtemp[0]+"/"+strtemp[1]+"/"+strtemp[2];
		}else{
			getDateStr = "----/--/--";
		}
		return getDateStr;
	}
	
	public String[] dateSub(String datestr) {
		String[] strtemp = new String[3];
		if(datestr.length()==8){
			strtemp[0] = datestr.substring(0,4);
			strtemp[1] = datestr.substring(4,6);
			strtemp[2] = datestr.substring(6,8);
		}else{
			strtemp[0] = "----";
			strtemp[1] = "--";
			strtemp[2] = "--";
		}
		return strtemp;
	}
	
	public int getStatus(String currStatus) {
		if(currStatus.matches("回答待ち")){
			return 0;
		}
		else if(currStatus.matches("回答済")){
			return 1;
		}else if(currStatus.matches("確認完了")){
			return 2;
		}else{
			return -1;
		}
	}
}
